package com.ypj.indusynergyems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ypj.indusynergyems.dao.entity.CompanyEntity;
import com.ypj.indusynergyems.dao.entity.DeptEntity;
import com.ypj.indusynergyems.dao.entity.PostEntity;
import com.ypj.indusynergyems.dao.entity.UserEntity;
import com.ypj.indusynergyems.dao.mapper.CompanyMapper;
import com.ypj.indusynergyems.dao.mapper.DeptMapper;
import com.ypj.indusynergyems.service.ICompanyService;
import com.ypj.indusynergyems.service.IUserService;
import com.ypj.indusynergyems.toolkit.RandomGenerator;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper,CompanyEntity> implements ICompanyService {

    private final IUserService userService;

    private final DeptMapper deptMapper;


    @Override
    public void register(CompanyEntity company) {
        String companyNum;
        do {
            companyNum = RandomGenerator.generateRandom();
        }while (!getCompanyNum(companyNum));
        company.setCompanyNum(companyNum);
        //将注册公司加入公司
        baseMapper.insert(company);
        //将公司num加入注册人账户
        LambdaUpdateWrapper<UserEntity> updateWrapper = Wrappers.lambdaUpdate(UserEntity.class)
                .eq(UserEntity::getPhone, company.getPhone());
        UserEntity user = UserEntity.builder()
                .companyNum(companyNum)
                .build();
        userService.update(user,updateWrapper);
    }

    @Override
    public List<CompanyEntity> findAllCompany(String phone) {
        LambdaQueryWrapper<CompanyEntity> queryWrapper = Wrappers.lambdaQuery(CompanyEntity.class)
                .eq(CompanyEntity::getPhone, phone)
                .eq(CompanyEntity::getStatus, 0);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<DeptEntity> findAllDept(int companyId) {
        LambdaQueryWrapper<DeptEntity> queryWrapper = Wrappers.lambdaQuery(DeptEntity.class)
                .eq(DeptEntity::getCompanyId, companyId)
                .eq(DeptEntity::getStatus, 0);
        return deptMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserEntity> findAllStaff(int companyNum) {
        LambdaQueryWrapper<UserEntity> queryWrapper = Wrappers.lambdaQuery(UserEntity.class)
                .eq(UserEntity::getCompanyNum, companyNum);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public int addDept(DeptEntity dept) {
        return deptMapper.insert(dept);
    }

    @Override
    public DeptEntity findMenuDetail(int deptId) {
        LambdaQueryWrapper<DeptEntity> queryWrapper = Wrappers.lambdaQuery(DeptEntity.class)
                .eq(DeptEntity::getDeptId, deptId)
                .eq(DeptEntity::getStatus, 0);
        return deptMapper.selectOne(queryWrapper);
    }

    @Override
    public int editMenu(DeptEntity dept) {
        LambdaUpdateWrapper<DeptEntity> updateWrapper = Wrappers.lambdaUpdate(DeptEntity.class)
                .eq(DeptEntity::getDeptId, dept.getDeptId())
                .eq(DeptEntity::getStatus, 0);
        return deptMapper.update(dept,updateWrapper);
    }

    @Override
    public String deleteMenu(int deptId) {
        LambdaUpdateWrapper<DeptEntity> updateWrapper = Wrappers.lambdaUpdate(DeptEntity.class)
                .eq(DeptEntity::getDeptId, deptId)
                        .eq(DeptEntity::getStatus,0);
        DeptEntity dept = DeptEntity.builder()
                .status(1)
                .build();
        int update = deptMapper.update(dept, updateWrapper);
        return update>0?"success":"fail";
    }

    @Override
    public CompanyEntity findCompanyDetail(int companyId) {
        LambdaQueryWrapper<CompanyEntity> queryWrapper = Wrappers.lambdaQuery(CompanyEntity.class)
                .eq(CompanyEntity::getCompanyId, companyId)
                .eq(CompanyEntity::getStatus, 0);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public int editCompany(CompanyEntity company) {
        LambdaUpdateWrapper<CompanyEntity> updateWrapper = Wrappers.lambdaUpdate(CompanyEntity.class)
                .eq(CompanyEntity::getCompanyId, company.getCompanyId())
                .eq(CompanyEntity::getStatus, 0);

        return baseMapper.update(company,updateWrapper);
    }

    @Override
    public String deleteCompany(int companyId) {
        LambdaUpdateWrapper<CompanyEntity> updateWrapper = Wrappers.lambdaUpdate(CompanyEntity.class)
                .eq(CompanyEntity::getCompanyId, companyId)
                .eq(CompanyEntity::getStatus,0);
        CompanyEntity company = CompanyEntity.builder()
                .status(1)
                .build();
        int update = baseMapper.update(company, updateWrapper);
        return update>0?"success":"fail";
    }

    public boolean getCompanyNum(String companyNum){
        LambdaQueryWrapper<CompanyEntity> queryWrapper = Wrappers.lambdaQuery(CompanyEntity.class)
                .eq(CompanyEntity::getCompanyNum, companyNum)
                .eq(CompanyEntity::getStatus, 0);
        CompanyEntity hasNumFlag = baseMapper.selectOne(queryWrapper);
        return hasNumFlag == null;
    }
}
